home *** CD-ROM | disk | FTP | other *** search
- #include <X11/Intrinsic.h>
- #include <stdlib.h>
-
- #include "MyCanvas.h"
- #include "monitor.h"
-
- #define AWT_LOCK() awt_lock_enter()
- #define AWT_UNLOCK() awt_lock_exit()
-
-
- struct ComponentData {
- Widget widget;
- int repaintPending;
- int x1, y1, x2, y2;
- };
-
- struct CanvasData {
- struct ComponentData comp;
- Widget shell;
- int flags;
- };
-
- void glxmotif(int);
-
-
- JNIEXPORT void JNICALL Java_MyCanvas_X11DrawGL
- (JNIEnv *env, jobject this, jint pData, jint x0, jint y0, jint x1, jint y1)
- {
-
- struct CanvasData *wdata;
- Window w;
- Display *d;
- GC gc;
- XWindowAttributes wattr;
-
- wdata = (struct CanvasData*)pData;
- glxmotif(pData);
- }
-
- #include <stdio.h>
- #include <Xm/Form.h>
- #include <Xm/Frame.h>
- #include <Xm/DrawingA.h>
- #include <X11/keysym.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glx.h>
- #include <X11/GLw/GLwMDrawA.h> /* Motif OpenGL drawing area. */
-
-
- static int snglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
- static int dblBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
-
- Display *dpy;
- GLboolean doubleBuffer = GL_TRUE, viewportUpdateNeeded = GL_TRUE, spinning = GL_FALSE;
- XtAppContext app;
- XtWorkProcId workId = 0;
- Widget toplevel, form, frame, glxarea;
- int gwidth, gheight;
-
- void
- updateViewport(Widget w)
- {
- Dimension width, height;
-
- AWT_LOCK();
- XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, NULL);
- glViewport(0, 0, (GLint) width, (GLint) height);
- viewportUpdateNeeded = GL_FALSE;
- AWT_UNLOCK();
-
- }
-
- void
- draw(Widget w)
- {
- if (viewportUpdateNeeded) updateViewport(w);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glBegin(GL_POLYGON);
- glColor3f(0.0, 0.0, 0.0); glVertex3f(-10.0, -10.0, 0.0);
- glColor3f(0.7, 0.7, 0.7); glVertex3f(10.0, -10.0, 0.0);
- glColor3f(1.0, 1.0, 1.0); glVertex3f(-10.0, 10.0, 0.0);
- glEnd();
- glBegin(GL_POLYGON);
- glColor3f(1.0, 1.0, 0.0); glVertex3f(0.0, -10.0, -10.0);
- glColor3f(0.0, 1.0, 0.7); glVertex3f(0.0, -10.0, 10.0);
- glColor3f(0.0, 0.0, 1.0); glVertex3f(0.0, 5.0, -10.0);
- glEnd();
- glBegin(GL_POLYGON);
- glColor3f(1.0, 1.0, 0.0); glVertex3f(-10.0, 6.0, 4.0);
- glColor3f(1.0, 0.0, 1.0); glVertex3f(-10.0, 3.0, 4.0);
- glColor3f(0.0, 0.0, 1.0); glVertex3f(4.0, -9.0, -10.0);
- glColor3f(1.0, 0.0, 1.0); glVertex3f(4.0, -6.0, -10.0);
- glEnd();
- if (doubleBuffer) {
- AWT_LOCK();
- glXSwapBuffers(dpy, XtWindow(w));
- AWT_UNLOCK();
- }
- glFlush();
- }
-
- void
- expose(Widget w, XtPointer clientData, XtPointer callData)
- {
- AWT_LOCK();
- draw(w);
- AWT_UNLOCK();
- }
-
- void
- resize(Widget w, XtPointer clientData, XtPointer callData)
- {
- XmDrawingAreaCallbackStruct *cd = (XmDrawingAreaCallbackStruct *) callData;
-
- AWT_LOCK();
- /* don't try OpenGL until window is realized! */
- if (XtIsRealized(w)) updateViewport(w);
- else viewportUpdateNeeded = GL_TRUE;
- AWT_UNLOCK();
-
- }
-
- void
- glxmotif(int pData)
- {
- XVisualInfo *vi;
- Colormap cmap;
- GLXContext cx;
- int saved_argc;
- String *saved_argv;
-
- struct CanvasData *wdata;
- Window w;
- Display *d;
- GC gc;
- XWindowAttributes wattr;
-
- wdata = (struct CanvasData*)pData;
- toplevel = wdata->comp.widget;
-
- AWT_LOCK();
-
- dpy = XtDisplay(wdata->comp.widget);
-
- /* find an OpenGL-capable RGB visual with depth buffer */
- vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);
- if (vi == NULL) {
- vi = glXChooseVisual(dpy, DefaultScreen(dpy), snglBuf);
- if (vi == NULL) XtAppError(app, "no RGB visual with depth buffer");
- doubleBuffer = GL_FALSE;
- }
- cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
-
- XGetWindowAttributes(dpy, XtWindow(toplevel),
- &wattr);
-
- gwidth = wattr.width;
- gheight = wattr.height;
-
- form = XmCreateForm(toplevel, "form", NULL, 0);
- XtManageChild(form);
-
- XtVaSetValues(form, XtNwidth, gwidth, XtNheight, gheight, NULL);
-
- frame = XmCreateFrame(form, "frame", NULL, 0);
- XtVaSetValues(frame, XmNbottomAttachment, XmATTACH_FORM,
- XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
- XmNrightAttachment, XmATTACH_FORM, NULL);
- XtManageChild(frame);
-
- XtVaSetValues(frame, XtNwidth, gwidth, XtNheight, gheight, NULL);
-
- glxarea = XtVaCreateManagedWidget("glxarea",
- glwMDrawingAreaWidgetClass, frame,
- GLwNvisualInfo, vi,
- XtNwidth, gwidth,
- XtNheight, gheight,
- XtNcolormap, cmap,
- NULL);
-
- #if 0
- XtAddCallback(glxarea, XmNexposeCallback, expose, NULL);
- XtAddCallback(glxarea, XmNresizeCallback, resize, NULL);
- #endif
-
-
- /* create an OpenGL rendering context */
- cx = glXCreateContext(dpy, vi, /* no display list sharing */ None, /* favor direct */ GL_TRUE);
- if (cx == NULL)
- XtAppError(app, "could not create rendering context");
-
- if (glXMakeCurrent(dpy, XtWindow(glxarea), cx))
- XtAppError(app, "could not bind OpenGL to rendering context");
-
- /* setup OpenGL state */
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- glClearDepth(1.0);
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glLoadIdentity();
- gluPerspective(40.0, 1.0, 10.0, 200.0);
- glTranslatef(0.0, 0.0, -50.0);
- glRotatef(-58.0, 0.0, 1.0, 0.0);
-
- /* draw into the glxarea */
- draw(glxarea);
-
- AWT_UNLOCK();
-
- }
-